<?php

class Admin_VideoController extends Zend_Controller_Action
{

	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}
	
    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->_model = new Default_Model_DbTable_Video();
    	$this->view->fullName = $infoUser->USERNAME;
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Video","PREPEND");
        $this->view->title = video_index_head;
        $video = $this->_model->GetAll();
        $paginator = Zend_Paginator::factory($video);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allVideo = $paginator;
    }
    
    public function addAction(){
    	$this->view->headTitle(video_add_title,"PREPEND");
    	$this->view->title = video_add_title;
    	$upload = new Private_Upload();
    	$lib = new Admin_Model_DbTable_LibraryVideo();
    	$this->view->LibVideo = $lib->getAllLibVideo();
    	if($this->getRequest()->isPost()){
    		$VName = $this->_request->getParam("VName");
    		$link = $this->_request->getParam("Link");
    		$stt = $this->_request->getParam("Stt");
    		$VLibId = $this->_request->getParam("VLib");
    	if($_FILES['VImg']['name']==null && $this->_request->getParam("textImg") == ""){
    			$img = "/ishali/public/uploaded/images/no_image.jpg";
    			$this->_model->AddVideo($VName, $link, $stt, $VLibId , $img);
    			$this->_redirect("/admin/video");
    		}elseif($this->_request->getParam("textImg")){
    			$img = $this->_request->getParam("textImg");
    			$this->_model->AddVideo($VName, $link, $stt, $VLibId , $img);
    			$this->_redirect("/admin/video");
    		}
    		else{
    			$img = "/ishali/public/uploaded/images/video/".$_FILES['VImg']["name"];
    			$upload->upload("video");
    			$this->_model->AddVideo($VName, $link, $stt, $VLibId , $img);
    		$this->_redirect("/admin/video");
    		}
    	}
    }

    public function editAction(){
    	$this->view->headTitle("Chá»‰nh Sá»­a Video","PREPEND");
    	$this->view->title = "Chá»‰nh Sá»­a Video";
    	$upload = new Private_Upload();
    	$Id = $this->_getParam("id");
    	$video = $this->_model->GetByID($Id);
    	$this->view->video = $video;
    	$lib = new Admin_Model_DbTable_LibraryVideo();
    	$this->view->LibVideo = $lib->getAllLibVideo();
    	if($this->getRequest()->isPost()){
    		$VName = $this->_request->getParam("VName");
    		$link = $this->_request->getParam("Link");
    		$stt = $this->_request->getParam("Stt");
    		$VLibId = $this->_request->getParam("VLib");
    	if($_FILES['VImg']['name']==null && $this->_request->getParam("textImg") == ""){
    			$img = $video->IMG;
    			$this->_model->UpdateVideo($Id,$VName, $link, $stt, $VLibId , $img);
    			$this->_redirect("/admin/video");
    		}elseif($this->_request->getParam("textImg")->isValid){
    			$img = $this->_request->getParam("textImg");
    			$this->_model->UpdateVideo($Id,$VName, $link, $stt, $VLibId , $img);
    			$this->_redirect("/admin/video");
    		}
    		else{
    			$img = "/ishali/public/uploaded/images/video/".$_FILES['VImg']["name"];
    			$upload->upload("video");
    			$this->_model->UpdateVideo($Id,$VName, $link, $stt, $VLibId , $img);
    			$this->_redirect("/admin/video");
    		}
    	}
    }
    public function delAction(){
    	$this->_model->RemoveVideo($this->_getParam("id"));
    	$this->_redirect("/admin/video");
    }
    public function activeAction(){
    	$this->_model->Active($this->_getParam("id"));
    	$this->_redirect("/admin/video");
    }

}

